//
//  AddBankViewController.m
//  AllFamily
//
//  Created by chirui on 2018/6/7.
//  Copyright © 2018年 Shui. All rights reserved.
//

#import "AddBankViewController.h"

@interface AddBankViewController (){
    
    NSArray * bankArray;
    NSString * choose;
}
@property (weak, nonatomic) IBOutlet UILabel *bankName;
@property (weak, nonatomic) IBOutlet UITextField *nameText;
@property (weak, nonatomic) IBOutlet UITextField *cartNumber;
@property (weak, nonatomic) IBOutlet UITextField *addressText;

@end

@implementation AddBankViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    choose = @"0";
    TitleView(@"添加银行卡");
    [self getBankName];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_nav_renter" target:self action:@selector(backBtnClick)];
}

-(void)backBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)chooseBank:(id)sender {
    
    if (bankArray.count>0) {
        zySheetPickerView * pickerView = [zySheetPickerView ZYSheetStringPickerWithTitle:bankArray andHeadTitle:@"选择银行" Andcall:^(zySheetPickerView *pickerView, NSString *choiceString) {
            _bankName.text = choiceString;
            choose = @"1";
            [pickerView dismissPicker];
        }];
        [pickerView show];
    }else{
        
        [self getBankName];
    }
 }
    

- (IBAction)sureBtnClick:(id)sender {
    
    if ([choose isEqualToString:@"0"]) {
        
        SHuiMBhudTextTimeStr(@"请选择银行");
      
    }else if (_nameText.text.length<=0){
        
        SHuiMBhudTextTimeStr(@"请输入姓名");
    }else if (_cartNumber.text.length<=0){
        
        SHuiMBhudTextTimeStr(@"请输入卡号");
    }else if (_addressText.text.length<=0){
        
          SHuiMBhudTextTimeStr(@"请输入开户支行");
    }
    else{
        
        [self addBankRequest];
    }
    
    
}

-(void)getBankName{
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(get_bankName_api) andparameters:nil Success:^(id responseObject) {
        bankArray = responseObject[@"datas"][@"lists"];
        
    } failed:^(NSError *error) {
        SHuiMBhudTextNetError;
    }];
    
}

-(void)addBankRequest{
    
    NSDictionary * dic = @{@"mid":USER_DEFAULTGet(@"mid"),
                           @"token":USER_DEFAULTGet(@"token"),
                           @"bank_name":_bankName.text,
                           @"bank_username":_nameText.text,
                           @"bank_num":_cartNumber.text,
                           @"bank_open":_addressText.text
                           };
    [PostNetWorking postNetWorkingStr:RequestUrlStr(add_bankCard_api) andparameters:dic Success:^(id responseObject) {
       
        if (codeSucess) {
            
            SHuiMBhudTextTimeStr(@"添加成功");
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
    } failed:^(NSError *error) {
        SHuiMBhudTextNetError;
    }];
}



@end
